<template>
  <div class="add-dialog">
    <el-dialog
      :title="isChange ? '修改目录' : '新增目录'"
      :visible="value"
      width="26%"
      @close="resetForm"
    >
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" size="small">
  <el-form-item label="所属学科" prop="subjectID">
    <el-select v-model="ruleForm.subjectID" placeholder="请选择" style="width:100%">
      <el-option v-for="item in subjectList" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="目录名称" prop="directoryName">
    <el-input v-model="ruleForm.directoryName" placeholder="请输入目录名称"></el-input>
  </el-form-item>
  <el-form-item class="dialogButton" size="medium">
    <el-button @click="resetForm">取消</el-button>
    <el-button type="primary" @click="onSubmit('ruleForm')">确认</el-button>
  </el-form-item>
</el-form>
    </el-dialog>
  </div>
</template>
<script>
import { list } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/directorys'
import lodash from 'lodash'
export default {
  props: {
    isChange: {
      type: Boolean,
      required: true
    },
    rowDate: {
      type: Object,
      required: true
    },
    value: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      ruleForm: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'change' }
        ]
      },
      subjectList: [],
      myRoWDate: {}
    }
  },
  methods: {
    // Dialog 关闭时的处理方法
    handleClose (done) {
      this.$refs.ruleForm.resetFields()
      done()
    },
    // 重置/关闭表单
    resetForm () {
      this.$refs.ruleForm.resetFields()
      this.$emit('input', false)
    },
    onSubmit () {
      // 如果是修改
      if (this.isChange === true) {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            await update({ id: this.myRoWDate.id, ...this.ruleForm })
            this.resetForm()
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.ruleForm.subjectID = ''
            this.ruleForm.directoryName = ''
            this.$emit('Refresh')
          } else {
            return false
          }
        })
        // 如果是新增
      } else {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            await add(this.ruleForm)
            this.resetForm()
            this.$emit('Refresh')
          } else {
            return false
          }
        })
      }
    },
    // 获取所属学科列表
    async getSubjectlList () {
      const res = await list({ pagesize: 1000 })
      console.log(res.data.items)
      this.subjectList = res.data.items
    }
  },
  created () {
    console.log(this.isChange)
  },
  watch: {
    // 监视 value 数据的变化
    value: function (newValue, oldValue) {
      this.getSubjectlList()
      if (this.isChange === true) {
        this.myRoWDate = lodash.cloneDeep(this.rowDate)
        this.ruleForm.subjectID = this.myRoWDate.subjectID
        this.ruleForm.directoryName = this.myRoWDate.directoryName
        console.log(this.myRoWDate)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.add-dialog{
    .dialogButton{
        margin: 60px 0 0 0;
        text-align: right
    }
}
</style>
